home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10767 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  96 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: () syntax for constructor for an array?
  5. Message-ID: <marnoldDo18pM.L94@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <NICKC.96Mar7121149@uxe.liv.ac.uk>
  8. Date: Sun, 10 Mar 1996 03:36:58 GMT
  9. Sender: marnold@netcom20.netcom.com
  10.  
  11. nickc@liv.ac.uk (Spider plant breeding program) writes:
  12.  
  13. >I can initalise an array with:
  14.  
  15. >float f[2] = { 2.718, 3.142 };
  16.  
  17.  
  18. >Is there some form of C++ syntax that can be used in a constructor - ie:
  19.  
  20.  
  21. >class a
  22. >{
  23. >  float f[2];
  24.  
  25. >public:
  26. >  a() : f( /* What goes in here ? */ )
  27. >  {};
  28. >};
  29.  
  30. There is no syntax file this.  The closest you could get it is to use
  31. the fact that *structs* can be implicity copied to and from each other...
  32.  
  33. struct float_array { float element[2]; };   // array inside a struct
  34.  
  35. class a
  36. {
  37.   static float_array init = { { 1.0, 2.0 } };
  38.   
  39.   float_array f;
  40.  
  41. public:
  42.   a() : f(init)
  43.   {};
  44. };
  45.  
  46. Course, inside class a, you will have to refer to the members of the
  47. array like this...
  48.  
  49.   f.element[index]
  50.  
  51. ...a slight syntactic inconvenience.
  52.  
  53. The other approach in the use the Standard Template Library (STL) class 
  54. vector for your array of floats.  Objects of type vector can be intialized 
  55. with each other.  You can even initialize a vector with a normal array of
  56. floats...
  57.  
  58. class a
  59. {
  60.   static float init = { 1.0, 2.0 };
  61.  
  62.   vector<float> f;
  63.  
  64. public:
  65.   a() : f(f_init, f_init + 2)
  66.   {};
  67. };
  68.  
  69. And, then you could go back to using...
  70.    
  71.   f[index]
  72.  
  73.  
  74. The last suggestion is to just stop worrying about intializing your array
  75. in the intializer list and simply copy the initial values in the ctor body 
  76. (no STL or struct tricks either)...
  77.  
  78. class a
  79. {
  80.   float f[2];
  81.  
  82. public:
  83.   a() { f[0] = 1.0; f[1] = 2.0; };
  84. };
  85.  
  86.  
  87.  
  88. Regards,
  89. -------------------------------------------------------------------------
  90. Matt Arnold                       |        | ||| | |||| |  | | || ||
  91. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  92. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  93. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  94. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  95. -------------------------------------------------------------------------
  96.